package com.mlethe.library.multi.demo;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mlethe.library.multi.api.MultiApi;
import com.mlethe.library.multi.api.MultiMedia;
import com.mlethe.library.multi.api.OauthAction;
import com.mlethe.library.multi.api.listener.OnActionListener;
import com.mlethe.library.multi.demo.databinding.ActivityOauthBinding;

import java.util.Map;

/**
 * 授权登录
 */
public class OauthActivity extends AppCompatActivity {

    private ActivityOauthBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityOauthBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        OnActionListener listener = new OnActionListener() {
            @Override
            public void onComplete(@NonNull MultiMedia media, @NonNull Map<String, String> map) {
                Log.e("multi", "onComplete: " + media + ", map->" + map);
                Toast.makeText(OauthActivity.this, media + "授权完成：" + map, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(@NonNull MultiMedia media) {
                Log.e("multi", "onCancel: " + media);
                Toast.makeText(OauthActivity.this, media + "授权取消", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(@NonNull MultiMedia media, int code, String msg) {
                Log.e("multi", "onFailure: " + media + "，code:" + code + ", msg: " + msg);
                Toast.makeText(OauthActivity.this, media + "授权失败，code:" + code + ", msg: " + msg, Toast.LENGTH_SHORT).show();
            }
        };
        binding.wechatBtn.setOnClickListener(v -> {
            new OauthAction(MultiMedia.WECHAT)
                    .setScope("snsapi_userinfo") // 信息授权域，用“,”隔开
                    .setCallback(listener) // 回调监听
                    .oauth(this);
        });
        binding.alipayBtn.setOnClickListener(v -> {
            new OauthAction(MultiMedia.ALIPAY)
                    .setAuthInfo("") // 授权信息（服务器返回）
                    .setCallback(listener) // 回调监听
                    .oauth(this);
        });
        binding.qqBtn.setOnClickListener(v -> {
            new OauthAction(MultiMedia.QQ)
                    .setScope("all") // 信息授权域，用“,”隔开
                    .setCallback(listener) // 回调监听
                    .oauth(this);
        });
        binding.weiboBtn.setOnClickListener(v -> {
            new OauthAction(MultiMedia.WEIBO)
                    .setCallback(listener) // 回调监听
                    .oauth(this);
        });
        binding.unionPayBtn.setOnClickListener(v -> {
            new OauthAction(MultiMedia.UNION_PAY)
                    .setScope("scope.base") // 信息授权域，用“,”隔开
                    .setCallback(listener) // 回调监听
                    .oauth(this);
        });
        binding.facebookBtn.setOnClickListener(v -> {
            new OauthAction(MultiMedia.FACEBOOK)
                    .setScope("public_profile") // 信息授权域，用“,”隔开
                    .setCallback(listener) // 回调监听
                    .oauth(this);
        });
        binding.googleBtn.setOnClickListener(v -> {
            new OauthAction(MultiMedia.GOOGLE)
                    .setCallback(listener) // 回调监听
                    .oauth(this);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MultiApi.getInstance().resume();
    }
}
